package com.navi.loaddata.loadService.impl.baseData.cf;

import com.navi.loaddata.core.etlException.ETLException;
import com.navi.loaddata.core.gpEntity.MArrayParamD;
import com.navi.loaddata.core.gpEntity.MOpeD;
import com.navi.loaddata.core.mesEntity.MesCtPospairproduct;
import com.navi.loaddata.core.mesEntity.MesProducthistory;
import com.navi.loaddata.core.oracleEntity.cf.EtlCfCmnGlassV;
import com.navi.loaddata.core.oracleEntity.cf.EtlCfParamV;
import com.navi.loaddata.core.oracleEntity.cf.EtlCfStepV;
import com.navi.loaddata.core.repository.impl.MesRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jay
 */
@Service
public class ProcessBaseDataServiceCF {
    @Autowired
    private MesRepositoryImpl daomes;
    private static final Logger logger = LoggerFactory.getLogger(ProcessBaseDataServiceCF.class);

    public List<EtlCfCmnGlassV> processCommonGlass(List<MesProducthistory> commonGlassVList, int errorProcessArrayCmnGlassV) throws ETLException {
        List<EtlCfCmnGlassV> etlCfCmnGlassVList=new ArrayList<>();

        try {
            Map<String, MesCtPospairproduct> mapMesCt1 = new HashMap<>();
            String hql1="from MesCtPospairproduct where  FACTORYNAME = 'ACF' and ACTIONTYPE = 'UNPACK'";
            List<MesCtPospairproduct> listMesCt1 = daomes.find(MesCtPospairproduct.class,hql1);
            listMesCt1.forEach(mesCt1 -> {
                mapMesCt1.put(mesCt1.getDestproductspecname(),mesCt1);
            });

            Map<String, MesCtPospairproduct> mapMesCt2 = new HashMap<>();
            String hql2="from MesCtPospairproduct where  FACTORYNAME = 'OC' and ACTIONTYPE = 'STB1'";
            List<MesCtPospairproduct> listMesCt2 = daomes.find(MesCtPospairproduct.class,hql2);
            listMesCt2.forEach(mesCt2 -> {
                mapMesCt2.put(mesCt2.getDestproductspecname(),mesCt2);
            });

            commonGlassVList.forEach(cmnGlass -> {
                EtlCfCmnGlassV etlCfCmnGlassV = new EtlCfCmnGlassV();
                etlCfCmnGlassV.setGlassId(cmnGlass.getProductname());
                etlCfCmnGlassV.setProductId(cmnGlass.getProductspecname());
//                etlCfCmnGlassV.setProductVersion(cmnGlass.getProductspecversion());
                if(null!=mapMesCt1.get(cmnGlass.getProductname())&&!StringUtils.isEmpty(mapMesCt1.get(cmnGlass.getProductname()).getDestproductspecname())) {
                    etlCfCmnGlassV.setBareGlassProduct(mapMesCt1.get(cmnGlass.getProductname()).getDestproductspecname());
                }
//                etlCfCmnGlassV.setSourceProductAry(mapMesCt2.get(cmnGlass.getProductname()));

                etlCfCmnGlassVList.add(etlCfCmnGlassV);
            });
        }catch (Exception e){
            logger.error(errorProcessArrayCmnGlassV+":"+e.getMessage());
            logger.error(LoggerUtil.stackTraceToString(e));
            throw new ETLException(errorProcessArrayCmnGlassV+":"+e.getMessage());
        }
        return etlCfCmnGlassVList;
    }

    public List<EtlCfParamV> processArrayParamInfo(List<MArrayParamD> arrayParamList, int errorProcessArrayParamD) throws ETLException {
        List<EtlCfParamV> etlCfParamVList=new ArrayList<>();
        try{
            arrayParamList.forEach(param -> {
                EtlCfParamV etlCfParamV=new EtlCfParamV();
                etlCfParamV.setParamCollection(param.getParamCollection());
                etlCfParamV.setParamName(param.getParamName());
                etlCfParamV.setParamCollectionGroup(param.getParamCollectionGroup());
                etlCfParamV.setParamGroup(param.getParamGroup());
                etlCfParamVList.add(etlCfParamV);
            });
        }catch (Exception e){
            logger.error(errorProcessArrayParamD+":"+e.getMessage());
            logger.error(LoggerUtil.stackTraceToString(e));
            throw new ETLException(errorProcessArrayParamD+":"+e.getMessage());
        }
        return etlCfParamVList;
    }

    public List<EtlCfStepV> processArrayStep(List<MOpeD> mesStep, int errorProcessArrayStepV) throws ETLException {
        List<EtlCfStepV> etlCfStepVList=new ArrayList<>();
        try{
            mesStep.forEach(step -> {
                EtlCfStepV etlCfStepV=new EtlCfStepV();
                if(StringUtils.isEmpty(step.getOpeId())){
                    System.out.print("opeId is null");
                }
                etlCfStepV.setStepId(step.getOpeId());
                etlCfStepV.setStepDesc("OTHER");
                etlCfStepV.setStepGroup("OTHER");
                etlCfStepV.setStepSeq(step.getOpeId());
                etlCfStepV.setStepType("OTHER");
                etlCfStepVList.add(etlCfStepV);
            });
        }catch (Exception e){
            logger.error(errorProcessArrayStepV+":"+e.getMessage());
            logger.error(LoggerUtil.stackTraceToString(e));
            throw new ETLException(errorProcessArrayStepV+":"+e.getMessage());
        }
        return etlCfStepVList;
    }
}
